package cn.hh.pet.service.impl;


import cn.hh.basic.service.impl.BasicServiceImpl;
import cn.hh.basic.util.PageList;
import cn.hh.pet.domain.Pet;
import cn.hh.pet.domain.PetDetail;
import cn.hh.pet.mapper.PetDetailMapper;
import cn.hh.pet.mapper.PetMapper;
import cn.hh.pet.query.PetQuery;
import cn.hh.pet.service.IPetService;
import cn.hh.product.domain.ProductDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PetServiceImpl extends BasicServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Override
    public void add(Pet pet) {
        /*保存宠物信息并返回主键*/
        petMapper.add(pet);
        /*保存宠物详情*/
        PetDetail petDetail = pet.getDetail();
        if (petDetail.getId()!=null){
            petDetail.setPet_id(pet.getId());
            petDetailMapper.add(petDetail);
        }
    }

    @Override
    public void addOrUpdate(Pet pet) {
        // 1.增改pet
        if (pet.getId()==null){
            petMapper.add(pet);
            System.out.println("保存后的数据"+pet.getId());
        }else {
            petMapper.update(pet);
        }
        // 2.判断是否有petdetail，有才执行增改
        PetDetail petDetail = pet.getDetail();
        if (petDetail!=null){
            if (petDetail.getId()==null){
                petDetail.setPet_id(pet.getId());
                System.out.println("即将保存的数据"+petDetail);
                petDetailMapper.add(petDetail);
            }else {
                petDetailMapper.update(petDetail);
            }
        }
    }

    @Override
    public PetDetail loadDetailById(Long id) {
        return petDetailMapper.loadDetailById(id);
    }

    @Override
    public void onsaleByIds(Long[] ids) {
        petMapper.onsaleByIds(ids);
    }

    @Override
    public void offsaleByIds(Long[] ids) {
        petMapper.offsaleByIds(ids);
    }

    @Override
    public PageList<Pet> listqueryPage(PetQuery query) {
        Integer total=petMapper.queryCount(query);
        List<Pet> bags = petMapper.queryData(query);
        PageList<Pet> pageList = new PageList<>(total, bags);
        return pageList;
    }

    /*删除*/
    @Override
    public void deleteById(Long id) {
        petDetailMapper.deleteByProductId(id);
        petMapper.deleteById(id);
    }
}
